all files / textField/ enhancedTextarea.vue

8% Statements 2/25
0% Branches 0/12
0% Functions 0/7
4.55% Lines 1/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                                                                                                                       
//
//
//
//
//
//
//
 
export default {
  props: {
    placeholder: {
      type: String
    },
    value: {
      type: String
    },
    rows: {
      type: Number,
      default: 1
    },
    rowsMax: {
      type: Number
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    resizeTextarea () {
      let element = this.$refs.textarea
      if (!element) return
      let hiddenEl = this.$refs.textareaHidden
      let lineHeight = window.getComputedStyle(element, null).getPropertyValue('line-height')
      lineHeight = Number(lineHeight.substring(0, lineHeight.indexOf('px')))
      let pt = window.getComputedStyle(element, null).getPropertyValue('padding-top')
      pt = Number(pt.substring(0, pt.indexOf('px')))
      let pd = window.getComputedStyle(element, null).getPropertyValue('padding-bottom')
      pd = Number(pd.substring(0, pd.indexOf('px')))
      let minHeight = pd + pt + lineHeight * this.rows
      let maxHeight = pd + pt + lineHeight * (this.rowsMax || 0)
      let height = hiddenEl.scrollHeight
      element.style.height = (height < minHeight ? minHeight : height > maxHeight && maxHeight > 0 ? maxHeight : height) + 'px'
    },
    handleInput (e) {
      this.$emit('change', e.target.value)
    },
    handleFocus (e) {
      this.$emit('focus', e)
    },
    handleBlur (e) {
      this.$emit('blur', e)
    }
  },
  mounted () {
    this.resizeTextarea()
  },
  watch: {
    value (val, oldVal) {
      if (val === oldVal) return
      this.$nextTick(() => {
        this.resizeTextarea()
      })
    }
  }
}